package com.yootk.webflux.handler;

import com.yootk.vo.Message;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 消息处理类
 */
@Slf4j
@Component
public class MessageHandler2 {

    public Mono<Message> echoHandler(Message message) {
        log.info("【{}】业务层接收处理数据：{}", Thread.currentThread().getName(), message);
        message.setTitle("【" + Thread.currentThread().getName() + "】" + message.getTitle());
        message.setContent("【" + Thread.currentThread().getName() + "】" + message.getContent());
        return Mono.create(monoSink -> monoSink.success(message));  // 实现数据响应
    }

    public Flux<Message> listHandler(Message message) {     // 返回List集合
        List<Message> list = new ArrayList<>();
        for (int x = 0; x < 10; x++) {
            Message msg = new Message();
            msg.setTitle("【" + x + "】" + message.getTitle());
            msg.setContent("【" + x + "】" + message.getContent());
            msg.setPubdate(message.getPubdate());
            list.add(msg);
        }
        return Flux.fromIterable(list);
    }

    public Flux<Map.Entry<String, Message>> mapHandler(Message message) {   // 返回Map集合
        Map<String, Message> map = new HashMap<>();
        for (int x = 0; x < 10; x++) {
            Message msg = new Message();
            msg.setTitle("【" + x + "】" + message.getTitle());
            msg.setContent("【" + x + "】" + message.getContent());
            msg.setPubdate(message.getPubdate());
            map.put("yootk - " + x, msg);
        }
        return Flux.fromIterable(map.entrySet());
    }
}
